home *** CD-ROM | disk | FTP | other *** search
/ Network CD 2 / Network CD - Volume 2.iso / programs / internet / mail / smailsr2.lha / SMail / src / RCS / getpath.c,v < prev    next >
Encoding:
Text File  |  1993-11-30  |  4.5 KB  |  239 lines

  1. head    2.2;
  2. access;
  3. symbols
  4.     C_2:2.2
  5.     C_1:1.2;
  6. locks; strict;
  7. comment    @ * @;
  8.  
  9.  
  10. 2.2
  11. date    93.11.30.22.42.33;    author Aussem;    state Exp;
  12. branches;
  13. next    2.1;
  14.  
  15. 2.1
  16. date    93.11.23.19.58.39;    author Aussem;    state Exp;
  17. branches;
  18. next    1.3;
  19.  
  20. 1.3
  21. date    93.11.20.20.38.55;    author Aussem;    state Exp;
  22. branches;
  23. next    1.2;
  24.  
  25. 1.2
  26. date    93.09.18.16.47.47;    author Aussem;    state Exp;
  27. branches;
  28. next    1.1;
  29.  
  30. 1.1
  31. date    93.09.08.16.27.13;    author Aussem;    state Exp;
  32. branches;
  33. next    ;
  34.  
  35.  
  36. desc
  37. @analyse and return the path
  38. @
  39.  
  40.  
  41. 2.2
  42. log
  43. @fixed a bug in searching the last host in path file
  44. @
  45. text
  46. @/*
  47.  *  getpath.c
  48.  *
  49.  *  Routines to lookup path for an address
  50.  *
  51.  * This program is free software; you can redistribute it and/or
  52.  * modify it under the terms of the GNU General Public License as
  53.  * published by the Free Software Foundation; either version 2 of
  54.  * the License, or (at your option) any later version.
  55.  *
  56.  * This program is distributed in the hope that it will be useful,
  57.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  58.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  59.  * General Public License for more details.
  60.  *
  61.  * You should have received a copy of the GNU General Public License
  62.  * along with this program; if not, write to the Free Software
  63.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  64.  *
  65.  * $Log: getpath.c,v $
  66.  * Revision 2.1  1993/11/23  19:58:39  Aussem
  67.  * Version 2.0 check in
  68.  *
  69.  * Revision 1.3  1993/11/20  20:38:55  Aussem
  70.  * extended Debug options
  71.  *
  72.  * Revision 1.2  1993/09/18  16:47:47  Aussem
  73.  * insert GNU license text in the header
  74.  *
  75.  * Revision 1.1  1993/09/08  16:27:13  Aussem
  76.  * Initial revision
  77.  *
  78.  *
  79.  */
  80.  
  81. static char     *rcsid="$Id: getpath.c,v 2.1 1993/11/23 19:58:39 Aussem Exp Aussem $";
  82.  
  83. # include    <stdio.h>
  84. # include    <sys/types.h>
  85. # include    <ctype.h>
  86. # include    "defs.h"
  87.  
  88. extern enum edebug debug;    /* how verbose we are         */ 
  89. extern char *pathfile;        /* location of path database    */
  90.  
  91. /*
  92. **
  93. ** getpath(): look up key in ascii sorted path database.
  94. **
  95. */
  96.  
  97. getpath( key, path , cost)
  98. char *key;        /* what we are looking for */
  99. char *path;        /* where the path results go */
  100. int *cost;        /* where the cost results go */
  101. {
  102.     long pos, middle, hi, lo;
  103.     static long pathlength = 0;
  104.     register char *s;
  105.     int c;
  106.     static FILE *file;
  107.     int flag;
  108.  
  109. print_error(NOTICE,"looking for '%s'\n", key);
  110.  
  111.     if(pathlength == 0) {    /* open file on first use */
  112.         if((file = fopen(pathfile, "r")) == NULL) {
  113.             (void) printf("can't access %s.\n", pathfile);
  114.             pathlength = -1;
  115.         } else {
  116.             (void) fseek(file, 0L, 2);    /* find length */
  117.             pathlength = ftell(file);
  118.         }
  119.     }
  120.     if( pathlength == -1 )
  121.         return( EX_OSFILE );
  122.  
  123.     lo = 0;
  124.     hi = pathlength;
  125.     (void) strcpy( path, key );
  126.     (void) strcat( path, "\t" );
  127. /*
  128. ** "Binary search routines are never written right the first time around."
  129. ** - Robert G. Sheldon.
  130. */
  131.     for( ;; ) {
  132.         pos = middle = ( hi+lo+1 )/2;
  133.         (void) fseek(file, pos, 0);    /* find midpoint */
  134.         if(pos != 0)
  135.             while(((c = getc(file)) != EOF) && (c != '\n'))
  136.                 ;    /* go to beginning of next line */
  137.         if(c == EOF) {
  138.             return(EX_NOHOST);
  139.         }
  140.         for( flag = 0, s = path; flag == 0; s++ ) { /* match??? */
  141.             if( *s == '\0' ) {
  142.                 goto solved;
  143.             }
  144.             if((c = getc(file)) == EOF) {
  145.             break; /* actions will be taken in "close window" part */
  146.             }
  147.             flag = lower(c) - lower(*s);
  148.         } 
  149.         if(lo >= middle) {        /* failure? */
  150.             return(EX_NOHOST);
  151.         }
  152.         if((c != EOF) && (flag < 0)) {    /* close window */
  153.             lo = middle;
  154.         } else {
  155.             hi = middle - 1;
  156.         }
  157.     }
  158. /* 
  159. ** Now just copy the result.
  160. */
  161. solved:
  162.     while(((c  = getc(file)) != EOF) && (c != '\t') && (c != '\n')) {
  163.         *path++ = c;
  164.     }
  165.     *path = '\0';
  166. /*
  167. ** See if the next field on the line is numeric.
  168. ** If so, use it as the cost for the route.
  169. */
  170.     if(c == '\t') {
  171.         int tcost = -1;
  172.         while(((c = getc(file)) != EOF) && isdigit(c)) {
  173.             if(tcost < 0) tcost = 0;
  174.             tcost *= 10;
  175.             tcost += c - '0';
  176.         }
  177.         if(tcost >= 0) *cost = tcost;
  178.     }
  179.     return (EX_OK);
  180. }
  181. @
  182.  
  183.  
  184. 2.1
  185. log
  186. @Version 2.0 check in
  187. @
  188. text
  189. @d21 3
  190. d36 1
  191. a36 1
  192. static char     *rcsid="$Id: getpath.c,v 1.3 1993/11/20 20:38:55 Aussem Exp Aussem $";
  193. d100 1
  194. a100 1
  195.                 return(EX_NOHOST);
  196. @
  197.  
  198.  
  199. 1.3
  200. log
  201. @extended Debug options
  202. @
  203. text
  204. @d21 3
  205. d33 1
  206. a33 1
  207. static char     *rcsid="$Id: getpath.c,v 1.2 1993/09/18 16:47:47 Aussem Exp Aussem $";
  208. @
  209.  
  210.  
  211. 1.2
  212. log
  213. @insert GNU license text in the header
  214. @
  215. text
  216. @d21 3
  217. d30 1
  218. a30 1
  219. static char     *rcsid="$Id: getpath.c,v 1.1 1993/09/08 16:27:13 Aussem Exp Aussem $";
  220. d58 1
  221. a58 1
  222. DEBUG("getpath: looking for '%s'\n", key);
  223. @
  224.  
  225.  
  226. 1.1
  227. log
  228. @Initial revision
  229. @
  230. text
  231. @d6 4
  232. a9 1
  233.  * $Log$
  234. d11 14
  235. d27 1
  236. a27 1
  237. static char     *rcsid="$Id$";
  238. @
  239.